package com.shinhye.smartsms.utils;

import java.io.InputStream;
import java.util.ArrayList;

import android.app.PendingIntent;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.Contacts;
import android.provider.ContactsContract.PhoneLookup;
import android.telephony.SmsManager;

public class CommonUtils {

	public static void printCursor(Cursor cursor) {
		while(cursor.moveToNext()) {
			int sum = cursor.getColumnCount();
			for(int i = 0; i < sum; i++) {
				String str = cursor.getString(i);
				System.out.println(cursor.getPosition() + "行:" + cursor.getColumnName(i) + ":" + str);
			}
		}
	}
	
	public static String getContactName(ContentResolver cr, String address) {
		 Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(address));
		 Cursor cursor = cr.query(uri, new String[]{"display_name"}, null, null, null);
		 String name = null;
		 if(cursor.moveToNext()) {
			 name = cursor.getString(0);
		 }
		 return name;
	}
	
	public static Bitmap getContactPhoto(ContentResolver cr, String address) {
		Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(address));
		Cursor cursor = cr.query(uri, new String[]{"_id"}, null, null, null);
		int _id = 0;
		if(cursor.moveToNext()) {
			_id = cursor.getInt(0);
		}
		Uri photoUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, _id);
		InputStream is = Contacts.openContactPhotoInputStream(cr, photoUri);
		Bitmap bmp = BitmapFactory.decodeStream(is);
		return bmp;
	}
	
	public static void sendSMS(Context context, String address, String text) {
		SmsManager sm = SmsManager.getDefault();
		ArrayList<String> list = sm.divideMessage(text);
		
		PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 010, new Intent("com.shinhye.smartsms.send"), PendingIntent.FLAG_ONE_SHOT);
		
		for(String tex : list) {
			sm.sendTextMessage(address, null, tex, pendingIntent, null);
		}
		
		ContentResolver cr = context.getContentResolver();
		ContentValues cv = new ContentValues();
		String numAddr = address.replaceAll("[-()]", "");
		cv.put("address", numAddr);
		cv.put("body", text);
		cv.put("date", System.currentTimeMillis());
		cv.put("type", 2);
		
		cr.insert(SmsUris.SMS_URI, cv);
	}
	
	public static int getContactByUri(ContentResolver cr, Uri uri) {
		Cursor cursor = cr.query(uri, new String[]{"_id", "has_phone_number"}, null, null, null);
		cursor.moveToNext();
		int hasNumber = cursor.getInt(1);
		if(hasNumber != 1) {
			return -1;
		} else {
			return cursor.getInt(0);
		}
	}
	
	public static String getAddressById(ContentResolver cr, int id) {
		Cursor cursor = cr.query(Phone.CONTENT_URI, new String[]{"data1"}, "contact_id = ?", new String[]{id + ""}, null);
		cursor.moveToNext();
		return cursor.getString(0);
	}
	
	public static Uri getUri(int type) {
		Uri[] uris = new Uri[]{
				SmsUris.SMS_INBOX_URI, SmsUris.SMS_OUTBOX_URI, SmsUris.SMS_SENT_URI, SmsUris.SMS_DRAFT_URI 
		};
		return uris[type];
	}
}
